<template>
  <div style="width: 100%;height: 100%;">
    <div id="luckysheet" v-loading="reportLoading" style="margin:0;padding:0;position:absolute;width:100%;top: 0;bottom:0;" />
  </div>
</template>

<script>
import { viewReport } from '@/api/dev/jmf/jmfReport'
import { exportExcel } from '@/views/dev/jmf/export'

export default {
  data() {
    var language = window.localStorage.getItem('language')
    switch (language) {
      case 'zh_cn':
        language = 'zh'
        break
      case 'en_us':
        language = 'en'
        break
    }
    return {
      reportLoading: false,
      reportId: this.$route.query.reportId,
      language: language
    }
  },
  mounted() {
    window.parent.luckysheet = window.luckysheet
    if (this.reportId) {
      this.$nextTick(() => {
        this.loadReport(this.reportId)
      })
    } else {
      this.initReport()
    }
  },
  methods: {
    initReport() {
      this.$nextTick(() => {
        window.luckysheet.destroy()
        const options = {
          container: 'luckysheet', // 设定DOM容器的id
          lang: this.language, // 设定表格语言
          plugins: ['chart'],
          showtoolbarConfig: { print: false },
          showinfobar: false // 是否显示顶部信息栏
        }
        window.luckysheet.create(options)
      })
    },
    loadReport(reportId) {
      console.log(window.luckysheet)
      this.$nextTick(() => {
        window.luckysheet.destroy()
        viewReport(reportId).then((res) => {
          this.reportLoading = true
          const options = {
            container: 'luckysheet', // 设定DOM容器的id
            lang: this.language, // 设定表格语言
            autoExpand: true, // 定制化内容
            forceCalculation: true, // 强制刷新公式
            plugins: ['chart'],
            data: res.data,
            showtoolbarConfig: { print: false },
            showinfobar: false // 是否显示顶部信息栏
          }
          // eslint-disable-next-line no-undef
          window.luckysheet.create(options)
          var th = this
          setTimeout(function() {
            th.reportLoading = false
            var allSheets = window.luckysheet.getAllSheets()
            for (let i = allSheets.length - 1; i >= 0; i--) {
              window.luckysheet.setSheetActive(i)
            }
          }, 2000)
          if (this.$route.query.export) {
            this.exportHandle()
          }
        })
      })
    },
    exportHandle() {
      const allSheets = window.luckysheet.getAllSheets()
      for (let i = 0; i < allSheets.length; i++) {
        allSheets[i].data = window.luckysheet.getSheetData({ order: i })
      }
      exportExcel(allSheets, this.$route.query.name)
    }
  }
}
</script>

<style scoped>

</style>
